package net.lovesrc.allin.jobs.backup;

import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.biz.treex.TreexRegisterService;
import net.lovesrc.allin.biz.treex.TreexService;
import net.lovesrc.allin.jobs.base.BaseJob;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Profile("live")
public class TreexAuthIdJob extends BaseJob {

    @Autowired
    private TreexRegisterService treexRegisterService;

    @Autowired
    private TreexService treexService;

    @Override
    public void runOnce() {
        // 实名
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select mobile from t_treex_account " +
                            " where idStatus = 'adv-submitted' " +
                            " and (idNextSyncTime is null or idNextSyncTime < ?) " +
                            " and isMyAccount = 0 " +
                            " order by idNextSyncTime asc limit 10",
                    new Date()
            );
            for (JSONObject row : rows) {
                String mobile = row.getString("mobile");
                treexRegisterService.authId(mobile);
            }
        }
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select mobile from t_treex_account " +
                            " where idStatus = 'basic-submitted' " +
                            " and (idNextSyncTime is null or idNextSyncTime < ?) " +
                            " and isMyAccount = 0 " +
                            " order by idNextSyncTime asc limit 10",
                    new Date()
            );
            for (JSONObject row : rows) {
                String mobile = row.getString("mobile");
                treexRegisterService.authId(mobile);
            }
        }
        // 初级实名
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select mobile from t_treex_account " +
                            " where idStatus is null and registerCode = 200 " +
                            " and insertTime < ? " +
                            " and isMyAccount = 0 " +
                            " order by rand()",
                    DateUtils.addMinutes(new Date(), -3)
            );
            for (JSONObject row : rows) {
                String mobile = row.getString("mobile");
                treexRegisterService.authId(mobile);
            }
        }
    }

}
